package com.hzqy.service.produceInfoJs;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.utils.FileUtil;
import com.hzqy.service.special.SpecialServiceBean;
import com.hzqy.web.login.SessionKey;
import com.hzqy.web.vo.PmsOrgVo;
import com.hzqy.web.vo.special.SpecialInfoVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.service.hqjc.HqjcServiceBean;
import com.hzqy.web.vo.CmContentVo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EducationCacheServiceBean {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private HqjcServiceBean hqjcServiceBean;

	@Resource
	private SpecialServiceBean specialServiceBean;
	public Object executeWrite(HttpServletRequest request, HttpSession session, CmContentVo contentVo) {
		// TODO Auto-generated method stub
		Map<String,Object> map=new HashMap<String, Object>();
		String path = contentVo.getF_cc_actors();
		String fileName = contentVo.getF_cc_name();
		int f_catalog_id = contentVo.getF_catalog_id();
		int pageNo = contentVo.getPageNo();
		if(pageNo>0){
			pageNo-=1;
		}
		int pageSize = contentVo.getPageSize();
		String f_cp_cprid = contentVo.getF_cp_cprid();
		String jsonName = contentVo.getF_cc_obligate1();
		String catalogs = contentVo.getF_sp_desc();//接收分类id 2,3,4 ，生成数据的key:catalog_2:
		LOG.debug("分类id:"+f_catalog_id+"-页码："+pageNo+"-每页数据量："+pageSize+"-类型："
				+f_cp_cprid+"-路径："+path+"-文件名称："+fileName+"-分类id列表:"+catalogs);
		contentVo.setF_cc_name("");
		contentVo.setF_cc_actors("");
		contentVo.setF_sp_desc("");
		String current_partner = (String)session.getAttribute(SessionKey.SESSION_PARTNER_ID);//当前平台 HW：华为, UT：UT2X, HW3:华为3.0, ZTE：中兴
		if("ZTE".equals(current_partner)){//中兴
			contentVo.setF_cc_interfaceType(2);
		}else {
			contentVo.setF_cc_interfaceType(3);
		}
		PmsOrgVo orgVo=(PmsOrgVo) session.getAttribute(SessionKey.SESSION_USER_FOR_ORG_INFO);
		String f_sa_id = String.valueOf(session.getAttribute(SessionKey.SESSION_AREA));
		contentVo.setF_sa_id(f_sa_id);
		int f_po_id=0;
		if(orgVo!=null && contentVo.getF_is_public()==0){
			f_po_id=orgVo.getF_po_id();
		}
		JSONObject json=new JSONObject();
		if(catalogs!=null && !"null".equals(catalogs) && !"".equals(catalogs)){
			String catids[] = catalogs.split(",");
			try {
				if(catids.length>0){
					for(int i=0;i<catids.length;i++){
						LOG.debug("要生成静态数据的分类id："+catids[i]);
						List<CmContentVo> dlist = hqjcServiceBean.selectContentListByCatalogId(Integer.parseInt(catids[i]),f_po_id+"", pageNo, pageSize,f_cp_cprid,contentVo);
						json.put("catalog_"+catids[i],dlist);
					}
				}
			}catch (Exception e){
				LOG.error("根据分类id列表生成静态数据失败:",e);
			}
		}else{
			List<CmContentVo> contentList = hqjcServiceBean.selectContentListByCatalogId(f_catalog_id,f_po_id+"", pageNo, pageSize,f_cp_cprid,contentVo);
			LOG.debug("数据大小:"+contentList.size());
			if(jsonName != null&&jsonName != "") {
				json.put(jsonName,contentList);
			}else {
				json.put("contentList",contentList);
			}
		}
		//String path=System.getProperty("user.dir")+"/school_hd/county_education_style_expand_js/"+fileName;
		//String path="/opt/MoNi/tomcat-82/webapps/web/school_hd/county_education_style_expand_js/"+fileName;
		String path_ = path + fileName;
		try {
			FileUtil.inputFileString(path_,"var infos_all="+json.toString());
			map.put("message","生成成功");
			map.put("json",json.toString());
		}catch (Exception e){
			LOG.error("生成静态文件失败:",e);
			map.put("message","生成失败");
		}
		return map;
	}

	/**
	 *生成专辑静态数据
	 * @param request
	 * @param session
	 * @param contentVo
	 * @return
	 */
	public Object createSpecialJs(HttpServletRequest request, HttpSession session, CmContentVo contentVo) {
		Map<String,Object> map=new HashMap<String, Object>();
		String path = contentVo.getF_cc_actors();// 传路径
		int f_cc_id = contentVo.getF_cc_id();//传专辑id
		LOG.debug("专辑id:"+f_cc_id+"-物理路径："+path);
		JSONObject json = new JSONObject();
		SpecialInfoVo specialInfoVo=specialServiceBean.selectSpecial(f_cc_id+"");
		json.put("specialInfoVo",specialInfoVo);
		//String path=System.getProperty("user.dir")+"/school_hd/county_education_style_expand_js/"+fileName;
		//String path="/opt/MoNi/tomcat-82/webapps/web/school_hd/county_education_style_expand_js/"+fileName;
		String path_ = path + "special_"+f_cc_id+".js";
		try {
			FileUtil.inputFileString(path_,"var infos_all="+json.toString());
			map.put("message","生成专辑数据成功");
			map.put("json",json.toString());
		}catch (Exception e){
			LOG.error("生成静态文件失败:",e);
			map.put("message","生成失败");
		}
		return map;
	}

	
}
